<?php

/**
 * 产品类别
 *
 * */
namespace app\admin\controller\product;


use app\admin\controller\Base;
use app\common\model\TouziProductClass as models;
use app\common\traites\PublicCrudTrait;
use app\validate\TouziProductClass as validates;
use think\exception\ValidateException;

class TouziProductClass extends Base
{
    protected $model;
    use PublicCrudTrait;

    public function initialize()
    {
        $this->model =new models();
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    /**
     *
     * 列表
     * */
    public function index(){
        //当前页
        $page = $this->request->post('page', 1);
        //每页显示数量
        $limit = $this->request->post('limit', 10);
        //查询搜索条件
        $post =$this->request->post();
        $map =[];
        $order = 'id desc';
        $list =$this->model->page_list($map,$limit,$page,$order);

        return $this->success($list);
    }
    /**
     * 新增
     * */
    public function add(){
        //过滤数据
        $postField = 'name,is_enable';
        $post = $this->request->only(explode(',', $postField), 'post', null);
        //验证数据
        try {
            validate(validates::class)->scene('add')->check($post);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $this->failed($e->getError());
        }
        //查询是否重复
        $find = $this->model->where('name', $post['name'])->find();
        if ($find) return $this->failed('产品类别名称已存在');
        //执行修改数据
        $save = $this->model->save($post);
        if ($save) return $this->success([]);
        return $this->failed('添加失败');
    }
    /**
     * 编辑
     * */
    public function edit(){
        //过滤数据
        $postField = 'id,name,is_enable';
        $post = $this->request->only(explode(',', $postField), 'post', null);
        //验证数据
        try {
            validate(validates::class)->scene('edit')->check($post);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $this->failed($e->getError());
        }
        //检测
        $find = $this->model->where('id', $post['id'])->find();
        if (!$find) return $this->failed('数据不存在');
        //执行修改数据
        $save = $this->model->update($post);
        if ($save) return $this->success([]);
        return $this->failed('修改失败');
    }
    /**
     * 删除
     * */
    public function del(){
        //过滤数据
        $postField ='id';
        $post = $this->request->only(explode(',', $postField), 'post', null);
        //验证数据
        try {
            validate(validates::class)->scene('del')->check($post);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $this->failed($e->getError());
        }
        //检测
        $find = $this->model->where('id', $post['id'])->find();
        if (!$find) return $this->failed('数据不存在');
        //执行修改数据
        $save = $this->model->where('id', $post['id'])->delete();
        if ($save) return $this->success([]);
        return $this->failed('删除失败');
    }
}